package com.tjp.eight.ui.fragment.tabvideo;

import com.tjp.eight.AllRetrofit;
import com.tjp.eight.ResultModel;
import com.tjp.eight.base.BasePresenterImpl;
import com.tjp.eight.constant.UrlConstant;
import com.tjp.eight.net.NetCallBack;
import com.tjp.eight.net.SubscribeCallBack;
import com.tjp.eight.ui.fragment.detail.ShareModel;
import com.tjp.eight.utils.AppUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.http.Url;

/**
 * Created by Administrator on 2017/2/13.
 */
public class VideoPresenter extends BasePresenterImpl<FVideoView> {
    private VideoService videoService = AllRetrofit.getRetrofit(UrlConstant.BaseUrl).create(VideoService.class);

    public VideoPresenter(FVideoView videoView) {
        attachView(videoView);
    }

    public void loadData(String json) {
        loadData(json, null);
    }

    private void loadData(String json, Object object) {
        RequestBody requestBody = AppUtils.getRequestBody(json);
        addSubscription(videoService.loadData(requestBody), new SubscribeCallBack<>(new NetCallBack<VideoResultModel>() {
            @Override
            public void onSuccess(VideoResultModel model) {
                view.loadData(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void updateViewCount(String json) {
        RequestBody req = AppUtils.getRequestBody(json);
        addSubscription(videoService.updateCount(req), new SubscribeCallBack<>(new NetCallBack<ResultModel>() {
            @Override
            public void onSuccess(ResultModel model) {
                view.updateViews(model);
            }

            @Override
            public void onFails(int code, String msg) {
               view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void updateZanCount(String json) {
        RequestBody req = AppUtils.getRequestBody(json);
        addSubscription(videoService.updateCount(req), new SubscribeCallBack<>(new NetCallBack<ResultModel>() {
            @Override
            public void onSuccess(ResultModel model) {
                view.updateZan(model);
            }

            @Override
            public void onFails(int code, String msg) {
                //view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void getShareData(String shareJson) {
        RequestBody req = AppUtils.getRequestBody(shareJson);
        addSubscription(videoService.getSharedata(req), new SubscribeCallBack<>(new NetCallBack<ShareModel>() {
            @Override
            public void onSuccess(ShareModel model) {
                view.shareSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void shareSuccess(String shareRJson) {
        RequestBody req = AppUtils.getRequestBody(shareRJson);
        addSubscription(videoService.sharesuccess(req), new SubscribeCallBack<>(new NetCallBack<ResultModel>() {
            @Override
            public void onSuccess(ResultModel model) {
                //view.shareSuccess(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void loadMData(String postJson) {
        RequestBody requestBody = AppUtils.getRequestBody(postJson);
        addSubscription(videoService.loadData(requestBody), new SubscribeCallBack<>(new NetCallBack<VideoResultModel>() {
            @Override
            public void onSuccess(VideoResultModel model) {
                view.loadMData(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }

    public void reLoadUrl(String vid) {
        addSubscription(videoService.reloadUrl(vid), new SubscribeCallBack<>(new NetCallBack<VideoReLoadUrlResultModel>() {
            @Override
            public void onSuccess(VideoReLoadUrlResultModel model) {
                view.reLoadUrl(model);
            }

            @Override
            public void onFails(int code, String msg) {
                view.loadFail(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }
}
